package com.uchedao.vehicledetection.utils.city;

import android.content.Context;
import android.content.res.AssetManager;

import com.uchedao.vehicledetection.utils.city.model.CityModel;
import com.uchedao.vehicledetection.utils.city.model.DistrictModel;
import com.uchedao.vehicledetection.utils.city.model.ProvinceModel;
import com.uchedao.vehicledetection.utils.city.service.XmlParserHandler;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by hang on 2015/9/14.
 */
public class CityUtil {

    private static CityUtil instance;

    private Context mContext;
    /**
     * 所有省
     */
    public List<String> mProvinceDatas;
    /**
     * key - 省 value - 市
     */
    public Map<String, ArrayList<String>> mCitisDatasMap = new HashMap<String, ArrayList<String>>();
    /**
     * key - 市 values - 区
     */
    public Map<String, ArrayList<String>> mAreaDatasMap = new HashMap<String, ArrayList<String>>();
    /**
     * key - 区 values - 邮编
     */
    public Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();

    private CityUtil(Context context) {
        this.mContext = context;
        initData();
    }

    public static CityUtil getInstance(Context context) {
        if(instance == null)
            instance = new CityUtil(context);
        return instance;
    }

    public ArrayList<String> getCityData(String province) {
        return mCitisDatasMap.get(province);
    }

    public void initData() {
        List<ProvinceModel> provinceList = null;
        AssetManager asset = mContext.getAssets();
        try {
            InputStream input = asset.open("province_data.xml");
            // 创建一个解析xml的工厂对象
            SAXParserFactory spf = SAXParserFactory.newInstance();
            // 解析xml
            SAXParser parser = spf.newSAXParser();
            XmlParserHandler handler = new XmlParserHandler();
            parser.parse(input, handler);
            input.close();
            // 获取解析出来的数据
            provinceList = handler.getDataList();
            mProvinceDatas = new ArrayList<String>();
            for (int i=0; i< provinceList.size(); i++) {
                // 遍历所有省的数据
                mProvinceDatas.add(provinceList.get(i).getName());
                List<CityModel> cityList = provinceList.get(i).getCityList();
                ArrayList<String> cityNames = new ArrayList<String>();
                for (int j=0; j< cityList.size(); j++) {
                    // 遍历省下面的所有市的数据
                    cityNames.add(cityList.get(j).getName());
                    List<DistrictModel> districtList = cityList.get(j).getDistrictList();
                    ArrayList<String> distrinctNameArray = new ArrayList<String>();
                    DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];
                    for (int k=0; k<districtList.size(); k++) {
                        // 遍历市下面所有区/县的数据
                        DistrictModel districtModel = new DistrictModel(districtList.get(k).getName(), districtList.get(k).getZipcode());
                        // 区/县对于的邮编，保存到mZipcodeDatasMap
                        mZipcodeDatasMap.put(districtList.get(k).getName(), districtList.get(k).getZipcode());
                        distrinctArray[k] = districtModel;
                        distrinctNameArray.add(districtModel.getName());
                    }
                    // 市-区/县的数据，保存到mDistrictDatasMap
                    mAreaDatasMap.put(cityNames.get(j), distrinctNameArray);
                }
                // 省-市的数据，保存到mCitisDatasMap
                mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {

        }
    }
}
